/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.editor.editparts;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Text;

/*
 * Adapted from code by Phil Zoio at http://www.eclipse.org/articles/Article-GEF-editor/gef-schema-editor.html
 */
public class ExtendedDirectEditManager extends DirectEditManager {

    Font figureFont;
    protected String originalValue;
    protected Label label;
    private boolean committing = false;

    public ExtendedDirectEditManager(GraphicalEditPart source,
            Class editorType, CellEditorLocator locator, Label label) {
        super(source, editorType, locator);
        this.label = label;
        originalValue = label.getText();
    }

    /**
     * @see org.eclipse.gef.tools.DirectEditManager#bringDown()
     */
    protected void bringDown() {
        Font disposeFont = figureFont;
        figureFont = null;
        super.bringDown();
        if (disposeFont != null)
            disposeFont.dispose();
    }

    /**
     * @see org.eclipse.gef.tools.DirectEditManager#initCellEditor()
     */
    protected void initCellEditor() {
        Text text = (Text) getCellEditor().getControl();

        // set the initial value of the
        originalValue = label.getText();
        getCellEditor().setValue(originalValue);

        // calculate the font size of the underlying
        IFigure figure = ((GraphicalEditPart) getEditPart()).getFigure();
        figureFont = figure.getFont();
        FontData data = figureFont.getFontData()[0];
        Dimension fontSize = new Dimension(0, data.getHeight());

        // set the font to be used
        // this.label.translateToAbsolute(fontSize);
        data.setHeight(fontSize.height);
        figureFont = new Font(null, data);

        text.setFont(figureFont);
        text.selectAll();
    }

    /**
     * Commits the current value of the cell editor by getting a {@link Command}
     * from the source edit part and executing it via the {@link CommandStack}.
     * Finally, {@link #bringDown()}is called to perform and necessary cleanup.
     */
    protected void commit() {
        if (committing)
            return;
        committing = true;
        try {
            // we set the cell editor control to invisible to remove any
            // possible flicker
            getCellEditor().getControl().setVisible(false);
            if (isDirty()) {
                CommandStack stack = getEditPart().getViewer().getEditDomain()
                        .getCommandStack();
                Command command = getEditPart().getCommand(
                        getDirectEditRequest());

                if (command != null && command.canExecute())
                    stack.execute(command);
            }
        } finally {
            bringDown();
            committing = false;
        }
    }

}
